package cn.tedu.superscholarstore.controller;


import cn.tedu.superscholarstore.mapper.ProductMapper;
import cn.tedu.superscholarstore.pojo.dto.ProductDto;
import cn.tedu.superscholarstore.pojo.dto.ProductUpdateDto;
import cn.tedu.superscholarstore.pojo.entity.Product;
import cn.tedu.superscholarstore.pojo.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.Date;
import java.util.List;

@RestController
public class ProductController {

    @Value("${dirPath}")
    private String dirPath;


    @Autowired(required = false)
    ProductMapper mapper;

    @RequestMapping("/product/insert")
    public void insert(@RequestBody ProductDto productDto){
        Product p = new Product();
        BeanUtils.copyProperties(productDto,p);
        p.setCreated(new Date());
        p.setViewCount(0);
        mapper.insert(p);
    }

    @RequestMapping("/product/select/admin")
    public List<ProductVo> select(){
        return mapper.select();
    }

    @RequestMapping("/product/delete/admin")
    public void delete(int id){
        String url = mapper.selectUrlById(id);
        new File(dirPath+url).delete();
        mapper.delete(id);
    }

    @RequestMapping("/product/selectById/admin")
    public ProductUpdateVo selectByIdUpdate(int id){
        return mapper.selectByIdUpdate(id);
    }

    @RequestMapping("/product/update/admin")
    public void update(@RequestBody ProductUpdateDto productUpdateDto){
        mapper.update(productUpdateDto);
    }

    @RequestMapping("/product/select/top")
    public List<ProductTopVo> selectByTop(){
           return mapper.selectByTop();
    }

    @RequestMapping("/product/select/index")
    public List<ProductIndexVo> selectIndex(){
           return mapper.selectIndex();
    }

    @RequestMapping("/product/selectById/detail")
    public ProductDetailVo selectByIdDetail(int id, HttpSession session){
        String info = (String) session.getAttribute("product"+id);
        if(info==null){
            mapper.updateViewCount(id);
            session.setAttribute("product"+id,"isVisited");
        }

         return mapper.selectDetailById(id);
    }

    @RequestMapping("/product/selectByCid")
    public List<ProductIndexVo>  selectByCid(int id){
        return mapper.selectByCid(id);
    }


    @RequestMapping("/product/selectByWd")
    public List<ProductIndexVo> selectByWd(String wd){
        return mapper.selectByWd(wd);
    }

    @RequestMapping("/product/newProduct/index")
    public List<ProductNewVo> selectByNew(){
        return mapper.selectByNew();
    }

    @RequestMapping("/product/hotProduct/index")
    public List<ProductNewVo> selectByHot(){
        return mapper.selectByHot();
    }

    @RequestMapping("product/loadding/index")
    public List<ProductIndexVo> loadding(int id){
        id = id*12;
        return mapper.selectByLoadding(id);
    }
}
